using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using TXLooker.Common;
using TXLooker.Common.Outlook;
using TXLooker.Core;
using TXLooker.Properties;
using TXLooker.WorkItemExtention;
using OutlookDataProvider=TXLooker.Core.OutlookDataProvider;
using TeamFoundationDataProvider=TXLooker.Core.TeamFoundationDataProvider;

internal class ApplicationGlobal
{
    // Fields
    private static string m_extentionDirectory = null;
    private static TXLooker.Core.LicenceDataProvider m_licenceDataProvider;
    private static OutlookDataProvider m_olDataProvider = null;
    private static List<MAPIFolder> m_projectFolders = new List<MAPIFolder>();
    private static IRibbonUI m_Ribbon;
    private static List<MAPIFolder> m_serverFolders = new List<MAPIFolder>();
    private static object m_syncWebViews = new object();
    private static TeamFoundationDataProvider m_tfDataProvider = null;
    private static Dictionary<WebViewType, string> m_webViews = new Dictionary<WebViewType, string>();
    private static ExtentionEntryPoint m_WorkItemTrakingExtention;

    // Methods
    public static string GetWebViewURL(WebViewType type)
    {
        if (m_webViews == null)
        {
            m_webViews = new Dictionary<WebViewType, string>();
            m_syncWebViews = new object();
        }
        lock (m_syncWebViews)
        {
            string path = null;
            bool flag = m_webViews.ContainsKey(type);
            if (flag)
            {
                path = m_webViews[type];
                if (File.Exists(path))
                {
                    return path;
                }
            }
            string htmlContent = null;
            switch (type)
            {
                case WebViewType.Root:
                    htmlContent = Resources.RootWebView;
                    break;

                case WebViewType.Server:
                    htmlContent = Resources.ServerWebView;
                    break;

                case WebViewType.Project:
                    htmlContent = Resources.ProjectWebView;
                    break;

                default:
                    throw new NotImplementedException();
            }
            path = OutlookGlobal.CreateFolderWebViewURL(htmlContent);
            if (flag)
            {
                m_webViews[type] = path;
            }
            else
            {
                m_webViews.Add(type, path);
            }
            return path;
        }
    }

    // Properties
    public static string ExtentionDirectiryPath
    {
        get
        {
            if (m_extentionDirectory == null)
            {
                m_extentionDirectory = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
                string str = Path.DirectorySeparatorChar.ToString();
                if (!m_extentionDirectory.EndsWith(str))
                {
                    m_extentionDirectory = m_extentionDirectory + str;
                }
            }
            return m_extentionDirectory;
        }
    }

    public static TXLooker.Core.LicenceDataProvider LicenceDataProvider
    {
        get
        {
            if (m_licenceDataProvider == null)
            {
                m_licenceDataProvider = new LicenceDataProvider();
            }
            return m_licenceDataProvider;
        }
    }

    public static string MacAddress
    {
        get
        {
            long num = Marshal.SizeOf(typeof(DllImportCommon.IP_ADAPTER_INFO));
            IntPtr pAdapterInfo = Marshal.AllocHGlobal(new IntPtr(num));
            int adaptersInfo = DllImportCommon.GetAdaptersInfo(pAdapterInfo, ref num);
            switch (adaptersInfo)
            {
                case 0x6f:
                    pAdapterInfo = Marshal.ReAllocHGlobal(pAdapterInfo, new IntPtr(num));
                    adaptersInfo = DllImportCommon.GetAdaptersInfo(pAdapterInfo, ref num);
                    break;

                case 0:
                    {
                        IntPtr ptr = pAdapterInfo;
                        DllImportCommon.IP_ADAPTER_INFO ip_adapter_info = (DllImportCommon.IP_ADAPTER_INFO)Marshal.PtrToStructure(ptr, typeof(DllImportCommon.IP_ADAPTER_INFO));
                        Marshal.FreeHGlobal(pAdapterInfo);
                        return ip_adapter_info.AdapterName.Replace("{", string.Empty).Replace("}", string.Empty);
                    }
            }
            Marshal.FreeHGlobal(pAdapterInfo);
            throw new InvalidOperationException("GetAdaptersInfo failed: " + adaptersInfo);
        }
    }

    public static OutlookDataProvider OlDataProvider
    {
        get
        {
            if (m_olDataProvider == null)
            {
                m_olDataProvider = new OutlookDataProvider();
            }
            return m_olDataProvider;
        }
    }

    public static List<MAPIFolder> ProjectFolders
    {
        get
        {
            return m_projectFolders;
        }
    }

    public static IRibbonUI RibbonUI
    {
        get
        {
            return m_Ribbon;
        }
        set
        {
            m_Ribbon = value;
        }
    }

    public static List<MAPIFolder> ServerFolders
    {
        get
        {
            return m_serverFolders;
        }
    }

    public static TXLooker.Core.TeamFoundationDataProvider TeamFoundationDataProvider
    {
        get
        {
            if (object.ReferenceEquals(m_tfDataProvider, null))
            {
                lock (typeof(ApplicationGlobal))
                {
                    if (object.ReferenceEquals(m_tfDataProvider, null))
                    {
                        m_tfDataProvider = new TXLooker.Core.TeamFoundationDataProvider();
                    }
                }
            }
            return m_tfDataProvider;
        }
    }

    public static ExtentionEntryPoint WorkItemTrakingExtention
    {
        get
        {
            return m_WorkItemTrakingExtention;
        }
        set
        {
            if (value == null)
            {
                throw new NullReferenceException();
            }
            m_WorkItemTrakingExtention = value;
        }
    }

    // Nested Types
    public enum WebViewType
    {
        Root,
        Server,
        Project
    }
}


